package com.cscd.controller;


import com.cscd.commonutils.JwtUtils;
import com.cscd.commonutils.R;
import com.cscd.entity.User;
import com.cscd.entity.vo.UserVo;
import com.cscd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chaichai
 * @since 2024-12-22
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public R login(@RequestBody User user, HttpServletRequest request){
        //   String userId = JwtUtils.getOpenidByJwtToken(request);
        R result = userService.login(user);
        return  result;
    }

    @PostMapping("/register")
    public R register(@RequestBody User user, HttpServletRequest request){
        //   String userId = JwtUtils.getOpenidByJwtToken(request);
        R result = userService.register(user);
        return  result;
    }
    @PostMapping("/logout")
    public R login(HttpServletRequest request){
        return  R.ok();
    }
    @GetMapping("/info")
    public R getUserInfo(HttpServletRequest request){
        String userId = JwtUtils.getUserIdByJwtToken(request);
        UserVo UserVo= userService.getUserInfo(userId);
        return  R.ok().data("userInfo",UserVo);
    }
    @GetMapping("/{id}")
    public R getUserById(@PathVariable String id, HttpServletRequest request){
        //   String userId = JwtUtils.getOpenidByJwtToken(request);
        R result = userService.getUserById(id);
        return  result;
    }
    @PostMapping("/list")
    public R getUserList(@RequestBody User user,@RequestParam(defaultValue = "1") Long pageNum,@RequestParam(defaultValue = "10") Long pageSize, HttpServletRequest request){
        R result = userService.getUserList(user,pageNum,pageSize);
        return  result;
    }
    @DeleteMapping("/delete/{ids}")
    public R deleteUsers(@PathVariable String[] ids, HttpServletRequest request){
        userService.deleteUsers(ids);
        return  R.ok();
    }
    @PostMapping("/update")
    public R updateUser(@RequestBody User user, HttpServletRequest request){
        userService.updateUser(user);
        return  R.ok();
    }
    @PostMapping("/add")
    public R addUser(@RequestBody User user, HttpServletRequest request){
        userService.addUser(user);
        return  R.ok();
    }

}

